package com.jiuyin.dianjing.ui.activity.team;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.bumptech.glide.Glide;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.MatchGameItemModel;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.fragment.team.GameGridFragment;
import com.jiuyin.dianjing.ui.fragment.team.TeamsFragment;
import com.jiuyin.dianjing.util.IMEUtil;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_MATCH_TYPE_CREATE;
import static com.jiuyin.dianjing.constant.HelperConstant.POSITION_DEFAULT;

public class JoinTeam1Activity extends BaseActivity {
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.toolbar)
    View toolbar;
    @BindView(R.id.view_alpha)
    View viewAlpha;
    @BindView(R.id.iv_back)
    View ivBack;
    @BindView(R.id.iv_xiala)
    View ivXiala;
    List<MatchGameItemModel> games = new ArrayList<>();
    private GameGridFragment gameGridFragment;
    private PopupWindow popupWindow;
    private GridView gamesGrid;
    private CommonAdapter<MatchGameItemModel> gamesAdapter;
    private int mSelectPosition = POSITION_DEFAULT;
    private String mGameId;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_join_team1;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initView() {
        TeamsFragment teamsFragment = TeamsFragment.newInstance("","", false);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.layout_teams, teamsFragment)
                .commit();
        ivBack.setOnClickListener(v -> {
            onBackPressed();
        });

        ViewGroup root = findViewById(R.id.layout_root);
        View popupView = getLayoutInflater().inflate(R.layout.fragment_game_grid, root, false);
        gamesGrid = popupView.findViewById(R.id.gv_rg_match_select);
        gamesGrid.setOnItemClickListener((parent, view, position, id) -> {
            if (mSelectPosition != position) {
                mSelectPosition = position;
                mGameId = gamesAdapter.getItem(position).getGameId();
            } else {
                mSelectPosition = POSITION_DEFAULT;
                mGameId = "";
            }
            teamsFragment.setGameId(mGameId);
            teamsFragment.refresh();
            popupWindow.dismiss();
        });
        gamesAdapter = new CommonAdapter<MatchGameItemModel>(this, R.layout.item_match_create, games) {
            @Override
            protected void convert(ViewHolder viewHolder, MatchGameItemModel item, int position) {
                viewHolder.setText(R.id.tv_match_create_name, item.getName());
                ImageView imageView = viewHolder.getView(R.id.img_match_create_icon);
                Glide.with(JoinTeam1Activity.this).load(item.getLogo()).into(imageView);
                if (position == mSelectPosition) {
                    viewHolder.setBackgroundRes(R.id.ll_game_info_item, R.drawable.item_match_game_select_select_bg);
                } else {
                    viewHolder.setBackgroundRes(R.id.ll_game_info_item, R.drawable.item_match_game_select_normal_bg);
                }
            }
        };
        gamesGrid.setAdapter(gamesAdapter);


        ivXiala.setOnClickListener(v -> {
            if (popupWindow == null) {
                popupWindow = new PopupWindow(popupView,
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);

                popupWindow.setFocusable(true);
                popupWindow.setOnDismissListener(() -> viewAlpha.setVisibility(View.GONE));
            }
            if (popupWindow.isShowing()) {
                popupWindow.dismiss();
            } else {
                IMEUtil.hideInputMethod(this);
                popupWindow.showAsDropDown(toolbar);
                viewAlpha.setVisibility(View.VISIBLE);
            }
        });

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                teamsFragment.setWord(s.toString());
                teamsFragment.refresh();
            }
        });

        fetchGames();
    }

    public void fetchGames() {
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, HelperApplication.getToken());
        params.put(KEY_TYPE, VALUE_MATCH_TYPE_CREATE);
        JSONObject jsonObject = new JSONObject(params);
        getApiHelper().appGetMyCompetitionGameList(jsonObject.toString(), data -> {
            games.clear();
            games.addAll(data);
            gamesAdapter.notifyDataSetChanged();
        });
    }

}
